package com.example.demo.Controller;

import com.github.pagehelper.PageInfo;
import com.example.demo.Entity.ClassInfo;
import com.example.demo.Entity.Student;
import com.example.demo.Service.ClassInfoService;
import com.example.demo.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.sql.ClientInfoStatus;
import java.util.List;

// 控制层，控制业务逻辑service，控制请求和响应，负责前后端交互 controller层主要调用Service层里面的接口控制具体的业务流程，控制的配置也要在配置文件中进行
@Controller
@RequestMapping("stu")
public class StudentController {

    @Autowired
    private StudentService studentService;  // Service类设计成一个接口，使控制层只依赖这个接口,当某天这个应用要跑在其它数据库上时，就而只需要增加一个serviceImpl类

    @Autowired
    private ClassInfoService classInfoService;

//    @GetMapping("/list")
//    public String getAllStudent(Model model){
//        List<Student> stuList = studentService.findAllStudent(pageIndex,pageSize);
//        List<ClassInfo> clsList =  classInfoService.findAllClassInfo();
//        model.addAttribute("clsinfo",clsList);
//        model.addAttribute("stus",stuList);
//        return "index";
//    }


    @RequestMapping("/list")
    public String getAllStudentByIDName(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                        @RequestParam(value = "clsid",defaultValue = "0") Integer clsid,
                                        @RequestParam(value = "stu_name",defaultValue = "") String stuName,
                                        Model model){

        PageInfo<Student> listInfo = null;
        if (clsid==0&&stuName.isEmpty()){
            listInfo = studentService.findAllStudent(pageIndex,pageSize);
            // lists = studentService.findAllStudent();
        }
        else if(clsid==0&&! stuName.isEmpty()){
            listInfo = studentService.findStudentByStuName(pageIndex,pageSize,stuName);
           // lists = studentService.findStudentByStuName(stuName);
        }
        else{
            listInfo = studentService.findStudentByClsIDStuName(pageIndex,pageSize,clsid,stuName);
            // lists = studentService.findStudentByClsIDStuName(clsid,stuName);
        }
        List<ClassInfo> clsList =  classInfoService.findAllClassInfo();
        model.addAttribute("stus",listInfo);
        model.addAttribute("clsinfo",clsList);
        model.addAttribute("clsid",clsid);
        model.addAttribute("stuname",stuName);
        return "main";
    }

    @GetMapping("/add")
    public String addStudentInfo(Model model){
        List<ClassInfo> lists = classInfoService.findAllClassInfo();
        model.addAttribute("clsinfo",lists);
        return "addstu";
    }

    @PostMapping("/add")
    public String addstudentInfo(Student student){
        studentService.addStudentInfo(student);
        return "redirect:/stu/list";
    }
    @GetMapping("/update/{id}")
    public String UpdateStudent(@PathVariable("id") Integer stuid, Model model){
        Student student = studentService.findStudentByID(stuid);
        List<ClassInfo> lists = classInfoService.findAllClassInfo();
        model.addAttribute("clsinfo",lists);
        model.addAttribute("stu",student);
        return "updatestu";
    }

    @PostMapping("/update")
    public String updateStudent(Student student){
        studentService.updateStudentID(student);
        return "redirect:/stu/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteStudent(@PathVariable("id") Integer stuid){
        studentService.deleteStudentInfo(stuid);
        return "redirect:/stu/list";
    }

}
